package org.vaadin.golfscoretracker.ui.components;

import org.vaadin.appfoundation.i18n.TranslationUtil;
import org.vaadin.golfscoretracker.data.Course;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

public class CourseFieldFactory extends DefaultFieldFactory {

    private static final long serialVersionUID = 7768049480848071366L;

    @SuppressWarnings("unchecked")
    @Override
    public Field createField(Item item, Object propertyId, Component uiContext) {
        Field field;
        if (propertyId.equals("name")) {
            TextField textField = new TextField();
            textField.setNullRepresentation("");
            field = textField;
        } else if (propertyId.equals("holes")) {
            Course course = ((BeanItem<Course>) item).getBean();
            HolesEditorField editor = new HolesEditorField(course);
            field = editor;
        } else {
            field = super.createField(item, propertyId, uiContext);
        }
        field.setCaption(TranslationUtil.getFieldTranslation(Course.class,
                (String) propertyId));
        return field;
    }

}
